package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.TargetDao;
import cn.gson.boot.model.mapper.TargetMapper;
import cn.gson.boot.model.pojos.SalesTargetVO;
import cn.gson.boot.model.pojos.Target;
import cn.gson.boot.model.pojos.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class TargetService {
    @Autowired
    TargetMapper targetMapper;//销售目标mapper

    @Autowired
    TargetDao targetDao;//销售目标dao

    //分页查询和模糊查询
    public List<Target> selecttarget(String targetCjsj,int empId){
        return targetMapper.selecttarget(targetCjsj,empId);
    }

    //统计图销售员（员工）的目标和业绩
    public Map<String,Object> empMbYj(String targetCjsj,int empId){
        List<Target> targetList = targetMapper.alltarget(empId);
        Map<String,Object> map = new HashMap<>();
        List<BigDecimal> mb = new ArrayList<>();//销售目标金额
        List<BigDecimal> yj = new ArrayList<>();//销售业绩金额
        List<String> mz = new ArrayList<>();//员工名字
        for(int i=0;i<targetList.size();i++){
            System.err.println("目标：QQQQQ！："+targetDao.empMbYj(targetList.get(i).getEmpId(),"%"+targetCjsj+"%"));
            mb.add(targetDao.empMbYj(targetList.get(i).getEmpId(),"%"+targetCjsj+"%"));
            yj.add(targetDao.empMbYj2(targetList.get(i).getEmpId(),"%"+targetCjsj+"%"));
            mz.add(targetList.get(i).getEmpId().getEmpName());
        }
        map.put("mb",mb);
        map.put("yj",yj);
        map.put("mz",mz);
        return map;
    }

    //折线统计图计算每个月的目标和业绩
    public Map<String,Object> MbYj(String nian){
        Map<String,Object> map = new HashMap<>();
        List<BigDecimal> mb = new ArrayList<>();//销售目标金额
        List<BigDecimal> yj = new ArrayList<>();//销售业绩金额
        String str = null;
        //循环12月
        for(int i=1;i<=12;i++){
            if(i<10){
                str = nian + "-0"+i;
            }else {
                str = nian + "-"+i;
            }
            mb.add(targetDao.Mb("%"+str+"%"));
            yj.add(targetDao.Yj("%"+str+"%"));
        }
        map.put("mb",mb);
        map.put("yj",yj);
        return map;
    }

    //销售目标新增
    public void addTarget(SalesTargetVO vo){
        System.out.println("开始新增");
        System.err.println("vo.getEmpId().getEmpId():"+vo.getEmpId().getEmpId());
        if(vo.getEmpId().getEmpId() == 0){
            vo.setEmpId(null);
        }
        List<Target> list = new ArrayList<>();
        for (int i = 1; i <= vo.getList().size();  i++ ){
            if (vo.getList().get(i-1) == null ){
                continue;
            }
            Target t = new Target();
            String bbb = vo.getTargetYear().substring(0,4);//截取年份
            String ccc = vo.getTargetYear().substring(7,vo.getTargetYear().length());//截取后面的是时分秒
            String sj = "";
            if (i<10){
                sj = bbb+"-0"+i+ccc;
            }else{
                sj =bbb+"-"+i+ccc;
            }
            t.setEmpId(vo.getEmpId());
            t.setTargetCjsj(Timestamp.valueOf(vo.getTargetYear()));//年份
            t.setTargetCjsj(Timestamp.valueOf(sj));//月份
            t.setTargetMb(new BigDecimal(vo.getList().get(i-1)));
            list.add(t);
        }
        targetDao.saveAll(list);
    }

}
